import 'package:file_transfer/layout/top_sliding_layout.dart';
import 'package:file_transfer/model/message_model.dart';
import 'package:file_transfer/model/simple_model.dart';
import 'package:file_transfer/pojo/message.dart';
import 'package:file_transfer/utils/toast_util.dart';
import 'package:file_transfer/widget/client_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class TopDrawerFragment extends StatefulWidget {
  const TopDrawerFragment({super.key});

  @override
  State<TopDrawerFragment> createState() => _TopDrawerFragmentState();
}

class _TopDrawerFragmentState extends State<TopDrawerFragment> {

  _selectClient(MessageModel messageModel,User user){
    messageModel.client = User(key: user.key,name: user.name);
    TopSlidingLayout.of(context).togglePanel();
  }

  _selectAllClient(MessageModel messageModel){
    if(messageModel.onlineUserList.isEmpty){
      ToastUtil.show("当前没有在线客户端！！");
      return;
    }
    messageModel.selectAllClient = true;
    TopSlidingLayout.of(context).togglePanel();
  }

  Widget _clientList(BuildContext context){
    final messageModel = context.watch<MessageModel>();
    final userList = messageModel.onlineUserList;
    return CustomScrollView(
      slivers: [
        SliverToBoxAdapter(
          child:GestureDetector(child: ClientWidget("所有用户",selected: messageModel.selectAllClient),
              onTap: ()=>_selectAllClient(messageModel)),
        ),
        SliverList(delegate: SliverChildBuilderDelegate((BuildContext context, int index){
          final user = userList[index];
          return GestureDetector(child: ClientWidget(user.name,key: Key(user.key),selected: !messageModel.selectAllClient && user.key == messageModel.client.key)
          ,onTap: ()=>_selectClient(messageModel,user),);
        },childCount: userList.length))
      ],
    );
  }


  @override
  Widget build(BuildContext context) {
    const radius = Radius.circular(60.0);
    return Container(
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(bottomLeft:radius ,bottomRight: radius)
      ),
      child: Column(
        children: [
          Expanded(child: _clientList(context)),
          LayoutBuilder(
            builder: (BuildContext context, BoxConstraints constraints){
              final margin = constraints.maxWidth/2*2/5;
              return Opacity(
                opacity: 0.13,
                child: Container(
                  height: 8,
                  margin: EdgeInsets.only(left: margin,right: margin,bottom: 20,top: 15),
                  decoration: BoxDecoration(
                      color: Colors.black,
                      borderRadius: BorderRadius.circular(360)
                  ),
                ),
              );
            },
          )
        ],
      ),
    );
  }
}
